import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    // 动态加载(懒加载)
    component: ()=>import (/* webpackChunkName: "login" */ '../views/Login.vue')
  },
  {
    path:'/home',
    redirect: '/home/welcome',
    component: ()=>import (/* webpackChunkName: "home" */ '../views/Home.vue'),
    children:[
      {
        path:'welcome',
        component: ()=>import (/* webpackChunkName: 'welcome' */ '../views/Users/Welcome.vue')
      },
      {
        path:'users',
        component: ()=>import (/* webpackChunkName: 'users' */ '../views/Users/Users.vue')
      },
      {
        path: 'rights',
        component: ()=>import (/* webpackChunkName: 'rights' */ '../views/Pights/Rights.vue')
      },
      {
        path: 'roles',
        component: ()=>import (/* webpackChunkName: 'roles' */ '../views/Pights/Roles.vue')
      },
      {
        path: 'categories',
        component: ()=>import (/* webpackChunkName: 'categories' */ '../views/Goods/Categories.vue')
      },
      {
        path: 'params',
        component: ()=>import (/* webpackChunkName: 'params' */ '../views/Goods/Params.vue')
      },
      {
        path: 'reports',
        component: ()=>import (/* webpackChunkName: 'reports' */ '../views/Reports/Reports.vue')
      },
      {
        path: 'orders',
        component: ()=>import (/* webpackChunkName: 'orders' */ '../views/Order/orders.vue')
      },
      {
        path: 'goods',
        component: ()=>import (/* webpackChunkName: 'goods' */ '../views/Goods/GoodList.vue')
      }
    ],
  },
]

const router = new VueRouter({
  routes
})
// 全局路由守卫
// 1.判断请求路径是否是/login 如果是则方形
// 2.如果不是/login 校验sessionStorage中有么有token
//  a.如果有方形
//  b.如果没有 则跳回登录页
router.beforeEach((to, from, next)=>{
  if(to.path == '/login'){
    next()
  }else{
    if(sessionStorage.getItem('token')){
      next()
    }
  }
})
export default router
